# 开发者：_Iamsleepingnow
# 开发时间：2023-10-24 21:19
# 开发功能：Noita存档备份程序
# encoding = utf-8
# -----------------------------------------
import os, shutil
from os import startfile
import sys
from PyQt5.QtWidgets import *
from NoitaMultiSLScriptUI import Ui_Form
# -----------------------------------------
ui = Ui_Form()
path_program = os.getenv("LOCALAPPDATA") + "Low" + "\\Nolla_Games_Noita" # 存档环境路径
path_save00 = path_program + "\\save00" # 存档路径
path_save00_SL_A = path_save00 + "_SL_A" # 存档备份路径A
path_save00_SL_B = path_save00 + "_SL_B" # 存档备份路径B
path_save00_SL_C = path_save00 + "_SL_C" # 存档备份路径C
path_save00_SL_Safety = path_save00 + "_SL_Safety" # 存档备份路径_安全模式（保证游戏存档不会被永久删除）
# -----------------------------------------
def event_OpenPath(self): # 打开存档路径
    if os.path.exists(path_program):  # 存档路径是否存在
        startfile(path_program)  # 打开文件夹窗口
# -----------------------------------------
def event_Save_A(self): # 备份存档
    if os.path.exists(path_program): # 存档路径是否存在
        ui.L_Record_A.setText("存在")
        # 如果存档环境路径中存在save00_SL，则将其删除
        if os.path.exists(path_save00_SL_A):
            shutil.rmtree(path_save00_SL_A)
        if os.path.exists(path_save00_SL_Safety):
            shutil.rmtree(path_save00_SL_Safety)
        # 将path_save00文件夹在原路径备份一份，并更名为save00_SL
        shutil.copytree(path_save00, path_save00_SL_A)
        shutil.copytree(path_save00, path_save00_SL_Safety)
def event_Load_A(self): # 替换存档
    if os.path.exists(path_program):  # 存档路径是否存在
        # 如果存档环境路径中存在save00_SL，则删除save00，复制save00_SL并重命名为save00
        if os.path.exists(path_save00_SL_A):
            shutil.rmtree(path_save00)
            shutil.copytree(path_save00_SL_A, path_save00)
def event_Clear_A(self): # 清除备份存档
    if os.path.exists(path_program):  # 存档路径是否存在
        # 如果存档环境路径中存在save00_SL，则将其删除
        if os.path.exists(path_save00_SL_A):
            shutil.rmtree(path_save00_SL_A)
            ui.L_Record_A.setText("空置")
# -----------------------------------------
def event_Save_B(self): # 备份存档
    if os.path.exists(path_program): # 存档路径是否存在
        ui.L_Record_B.setText("存在")
        # 如果存档环境路径中存在save00_SL，则将其删除
        if os.path.exists(path_save00_SL_B):
            shutil.rmtree(path_save00_SL_B)
        if os.path.exists(path_save00_SL_Safety):
            shutil.rmtree(path_save00_SL_Safety)
        # 将path_save00文件夹在原路径备份一份，并更名为save00_SL
        shutil.copytree(path_save00, path_save00_SL_B)
        shutil.copytree(path_save00, path_save00_SL_Safety)
def event_Load_B(self): # 替换存档
    if os.path.exists(path_program):  # 存档路径是否存在
        # 如果存档环境路径中存在save00_SL，则删除save00，复制save00_SL并重命名为save00
        if os.path.exists(path_save00_SL_B):
            shutil.rmtree(path_save00)
            shutil.copytree(path_save00_SL_B, path_save00)
def event_Clear_B(self): # 清除备份存档
    if os.path.exists(path_program):  # 存档路径是否存在
        # 如果存档环境路径中存在save00_SL，则将其删除
        if os.path.exists(path_save00_SL_B):
            shutil.rmtree(path_save00_SL_B)
            ui.L_Record_B.setText("空置")
# -----------------------------------------
def event_Save_C(self): # 备份存档
    if os.path.exists(path_program): # 存档路径是否存在
        ui.L_Record_C.setText("存在")
        # 如果存档环境路径中存在save00_SL，则将其删除
        if os.path.exists(path_save00_SL_C):
            shutil.rmtree(path_save00_SL_C)
        if os.path.exists(path_save00_SL_Safety):
            shutil.rmtree(path_save00_SL_Safety)
        # 将path_save00文件夹在原路径备份一份，并更名为save00_SL
        shutil.copytree(path_save00, path_save00_SL_C)
        shutil.copytree(path_save00, path_save00_SL_Safety)
def event_Load_C(self): # 替换存档
    if os.path.exists(path_program):  # 存档路径是否存在
        # 如果存档环境路径中存在save00_SL，则删除save00，复制save00_SL并重命名为save00
        if os.path.exists(path_save00_SL_C):
            shutil.rmtree(path_save00)
            shutil.copytree(path_save00_SL_C, path_save00)
def event_Clear_C(self): # 清除备份存档
    if os.path.exists(path_program):  # 存档路径是否存在
        # 如果存档环境路径中存在save00_SL，则将其删除
        if os.path.exists(path_save00_SL_C):
            shutil.rmtree(path_save00_SL_C)
            ui.L_Record_C.setText("空置")
# -----------------------------------------
def onInit():
    if os.path.exists(path_program): # 存档路径是否存在
        if os.path.exists(path_save00_SL_A):
            ui.L_Record_A.setText("存在")
        else:
            ui.L_Record_A.setText("空置")
        if os.path.exists(path_save00_SL_B):
            ui.L_Record_B.setText("存在")
        else:
            ui.L_Record_B.setText("空置")
        if os.path.exists(path_save00_SL_C):
            ui.L_Record_C.setText("存在")
        else:
            ui.L_Record_C.setText("空置")
    pass
# -----------------------------------------
if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = QMainWindow()
    ui.setupUi(mainWindow)
    mainWindow.show()
    mainWindow.setWindowTitle("Noita存档备份程序")
    # 注册信号槽
    ui.PB_OpenPath.clicked.connect(event_OpenPath)
    ui.PB_Save_A.clicked.connect(event_Save_A)
    ui.PB_Load_A.clicked.connect(event_Load_A)
    ui.PB_Clear_A.clicked.connect(event_Clear_A)
    ui.PB_Save_B.clicked.connect(event_Save_B)
    ui.PB_Load_B.clicked.connect(event_Load_B)
    ui.PB_Clear_B.clicked.connect(event_Clear_B)
    ui.PB_Save_C.clicked.connect(event_Save_C)
    ui.PB_Load_C.clicked.connect(event_Load_C)
    ui.PB_Clear_C.clicked.connect(event_Clear_C)
    #
    onInit()
    sys.exit(app.exec_())
# -----------------------------------------